Ext.define('YX.core.Main', {
	extend : 'Ext.container.Viewport',
	layout : {
		type : 'border',
		split : true,
		splitterResize : true
	},
	pelNav : null,
	pelNavBtn : null,
	btnUI : null,
	btnOI : null,
	btnChart : null,
	treeNav : null,
	frmFilterNav : null,
	txtFilter : null,
	pelMain : null,
	pelNavAllMenu : null,
	tabMain : null,
	requires : ['UX.codeview.FieldX'],
	statics : {
		addTab : function(cmp) {
			if (cmp) {
				var main = Ext.ClassManager.get('main');
				var destCmp = main.tabMain.getComponent(main.getItemId());
				if (!destCmp) {
					main.tabMain.add(cmp);
					destCmp = cmp;
				}

				main.tabMain.setActiveTab(destCmp);
			}
		}
	},
	initComponent : function() {
		this.btnUI = Ext.create('Ext.button.Button', {
			  scale : 'large',
			  width : 64,
			  icon : 'resources/' + appName + '/icon/run-build-2.png',
			  style : 'margin-left:3px;',
			  iconAlign : 'top',
			  enableToggle : true,
			  text : T.label.func,
			  scope : this,
			  handler : this.onBtnUIClicked
		  });

		this.btnOI = Ext.create('Ext.button.Button', {
			  scale : 'large',
			  width : 64,
			  icon : 'resources/' + appName + '/icon/player-time-2.png',
			  style : 'margin-left:3px;',
			  iconAlign : 'top',
			  enableToggle : true,
			  text : T.label.operate,
			  scope : this,
			  handler : this.onBtnOIClicked
		  });

		this.btnChart = Ext.create('Ext.button.Button', {
			  scale : 'large',
			  width : 64,
			  icon : 'resources/' + appName + '/icon/office-chart-pie.png',
			  style : 'margin-left:3px;margin-right:3px;',
			  iconAlign : 'top',
			  enableToggle : true,
			  text : T.label.chart,
			  scope : this,
			  handler : this.onBtnChartClicked
		  });

		this.pelNavBtn = Ext.create('Ext.panel.Panel', {
			  height : 64,
			  layout : {
				  type : 'hbox',
				  align : 'stretch',
				  pack : 'center',
				  flex : 1
			  },
			  items : [this.btnUI, this.btnOI, this.btnChart]
		  });

		this.treeNav = Ext.create('Ext.tree.Panel', {
			  flex : 1,
			  rootVisible : false,
			  root : {
				  expanded : true
			  },
			  store : Ext.create('Ext.data.TreeStore', {
				    fields : ['funCode', 'parentFunCode', 'asmName', 'asmFile',
				      'asmClassName', 'funType', 'funSide', 'iconPath', 'text'],
				    proxy : {
					    type : 'zbusrpc',
					    module : 'SecViewUserTreeFunctionService',
					    extraParams : {
						    funGroup : 'EXT',
						    funSide : 'U'
					    },
					    reader : {
						    type : 'json'
					    }
				    }
			    })
		  });

		this.pelNav = Ext.create('Ext.panel.Panel', {
			  region : 'west',
			  width : 210,
			  split : false,
			  title : T.label.systemFunction,
			  collapsible : true,
			  layout : {
				  type : 'vbox',
				  align : 'stretch'
			  },
			  items : [this.pelNavBtn, this.treeNav]
		  });

		this.tabMain = Ext.create('Ext.tab.Panel', {
			items : {
				closable : false,
				title : T.label.home,
				bodyStyle : 'background-image:url(resources/yxmes/images/laptop-1.png);background-repeat:no-repeat;background-position:center;'
			},
			plugins : Ext.create('Ext.ux.TabCloseMenu', {
				  showCloseAll : false,
				  extraItemsHead : [{
					    text : T.label.closeLeft
				    }, {
					    text : T.label.closeRight
				    }]
			  })
		});

		this.pelMain = Ext.create('Ext.panel.Panel', {
			  region : 'center',
			  layout : {
				  type : 'card'
			  },
			  items : [ // 功能操作界面容器
			  this.tabMain, { // 生产操作界面容器
				  xtype : 'panel',
				  layout : 'fit'
			  }, { // 图表界面容器
				    xtype : 'panel',
				    layout : 'fit'
			    }]
		  });

		this.pelBottom = new Ext.Panel({
			region : "south",
			height : 25,
			html : "<span style='font-size:12px;width:120px;line-height:25px;float:left;'>当前用户："
			  + LoginInfo.USERNAME
			  + "</span><span style='position: relative; left:50%;margin-left:-230px;width:300px; height: 25px;font-size:12px;line-height:25px;'>"
			  + "版权所有 © 2018-2020 深圳微邦软件信息有限公司.</span>"
		});

		this.items = [this.pelNav, this.pelMain, this.pelBottom];

		this.treeNav.on('itemclick', this.onTreeNavItemClick, this);
		this.on('afterrender', this.onAfterRender, this);
		this.tabMain.on('add', this.onTabMainAdded, this);

		this.callParent();
	},
	onAfterRender : function() {
		this.btnUI.toggle(true, true);
	},
	onTreeNavItemClick : function(tree, record, item) {
		if ('F' !== record.get('funType')) {
			return;
		}

		var cts = this.pelMain.getLayout().getLayoutItems();
		var me = this;
		if (record.get('funSide') == 'U') {
			this.pelMain.getLayout().setActiveItem(0);
			var destCmp = cts[0].getComponent(record.get('funCode'));
			if (!destCmp) {
				if (!Ext.isEmpty(record.get('asmFile'))) {
					var asmFile = record.get('asmFile').replace('/', '.');
					if (asmFile.toLowerCase().endsWith('.js')) {
						asmFile = asmFile.substring(0, asmFile.lastIndexOf('.js'));
					}
					var path = asmFile.substring(0, asmFile.lastIndexOf('.') + 1);
					asmFile = 'YX.' + asmFile;
					var className = Ext.String.startsWith(record.get('asmName'), 'YX.', true)
					  ? record.get('asmName')
					  : ('YX.' + path + record.get('asmName'));

					Ext.require(asmFile, function() {
						  var cmp = Ext.create(className);
						  destCmp = Ext.create('Ext.panel.Panel', {
							    itemId : record.get('funCode'),
							    title : record.get('text'),
							    layout : 'fit',
						  closable : true,
							    items : [cmp]
						    });
						  cts[0].add(destCmp);
						  cts[0].setActiveTab(destCmp);
					  });
				} else if (!Ext.isEmpty(record.get('asmClassName'))) {
					var asmClassName = record.get('asmClassName');
					if (asmClassName.indexOf('_') > 0
					  && asmClassName.indexOf('_') === asmClassName.lastIndexOf('_')) {
						asmClassName = appName + "."
						  + asmClassName.substring(0, asmClassName.indexOf('_')) + ".view."
						  + asmClassName.substr(asmClassName.indexOf('_') + 1);
					}

					// var viewIndex = asmClassName.lastIndexOf("View");
					var ctName = Ext.String.endsWith(asmClassName, 'View') ? asmClassName
					  .slice(0, -4) : asmClassName;
					ctName = ctName.concat('Controller').replace('.view.', '.controller.');

					var ctl = null;
					try {
						ctl = YXMES.getApplication().getController(ctName);
					} catch (ex) {
						Ext.Msg.alert('error', ex);
						throw ex;
					}

					var cmp = ctl.getView(Ext.String.endsWith(asmClassName, 'View')
					  ? asmClassName
					  : asmClassName.concat("View")).create();
					destCmp = Ext.create('Ext.panel.Panel', {
						  itemId : record.get('funCode'),title : record.get('text'),
						  layout : 'fit',
						  closable : true,
						  items : [cmp]
					  });

					cts[0].add(destCmp);
					cts[0].setActiveTab(destCmp);
					// ctl.init();
				}
			} else {
				cts[0].setActiveTab(destCmp);
			}
		} else if (record.get('funSide') == 'O') {
			this.pelMain.getLayout().setActiveItem(1);
		} else if (record.get('funSide') == 'C') {
			this.penMain.getLayout().setActiveItem(2);
		}
	},
	onBtnUIClicked : function(btn) {
		this.btnOI.toggle(false, true);
		this.btnChart.toggle(false, true);

		var proxy = this.treeNav.getStore().getProxy();
		proxy.setExtraParam('funSide', 'U');
		this.treeNav.getStore().reload();
	},
	onBtnOIClicked : function(btn) {
		this.btnUI.toggle(false, true);
		this.btnChart.toggle(false, true);

		var proxy = this.treeNav.getStore().getProxy();
		proxy.setExtraParam('funSide', 'O');
		this.treeNav.getStore().reload();
	},
	onBtnChartClicked : function(btn) {
		this.btnOI.toggle(false, true);
		this.btnUI.toggle(false, true);

		var proxy = this.treeNav.getStore().getProxy();
		proxy.setExtraParam('funSide', 'C');
		this.treeNav.getStore().reload();

	},
	onTabMainAdded : function(tabPanel, cmp) {
		this.setAuthority(cmp);
	},
	setAuthority : function(cmp) {
		if (cmp == null || LoginInfo.ORGCODE == "SYSTEM") {
			return;
		}

		var buttons = cmp.query("button");
		if (!Ext.isEmpty(buttons)) {
			var ctlBtns = [];
			Ext.each(buttons, function(btn) {
				  if (Ext.String.startsWith(btn.itemId, 'btn', true)) {
					  btn.disable();
					  ctlBtns.push(btn);
				  }
			  });

			if (!Ext.isEmpty(ctlBtns)) {
				YX.ZbusRpc.request({
					  module : 'SecViewUserFunctionControlService',
					  displayFail : false,
					  displaySuccess : false,
					  params : {
						  funCode : cmp.itemId
					  },
					  success : function(response) {
						  var ctlList = response.ctlList;
						  if (Ext.isEmpty(ctlList)) {
							  return;
						  }

						  Ext.each(ctlBtns, function(btn) {
							    Ext.each(ctlList, function(ctl) {
								      if (ctl.ctlName === btn.itemId) {
									      btn.setDisabled(ctl.hasCtl !== "Y");
									      return false;
								      }
							      });
						    });
					  }
				  });
			}
		}
	}
});
